package com.shimao.celnet.payment.quartz;


import com.shimao.celnet.payment.form.IInterfaceLogForm;
import com.shimao.celnet.payment.model.IInterfaceLog;
import com.shimao.celnet.payment.service.IInterfaceLogService;
import com.shimao.celnet.payment.util.HttpSendPostUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 用来做消息推送失败重推
 * 2018/03/29
 * christ
 */
@RestController
@RequestMapping("/batch")
public class FailueInfoPushAgain implements Job {

    @Autowired
    private HttpSendPostUtil httpSendPostUtil;
    @Autowired
    private IInterfaceLogService iInterfaceLogService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //1,每隔30秒扫描一次日志表，将推送失败的
        IInterfaceLogForm formBean = new IInterfaceLogForm();
        formBean.setAppName("支付系统推送");
        formBean.setInterfaceType("callOut");
        formBean.setRequestText("http");
        List<IInterfaceLog> iInterfaceLogsList = iInterfaceLogService.getInfoForPushAgain(formBean);
        //2，推送3次没成功就默认为失败
        for (IInterfaceLog iInterfaceLog : iInterfaceLogsList) {
            if (Integer.valueOf(iInterfaceLog.getRecallNumber() == null ? "0" : iInterfaceLog.getRecallNumber()) >= 3) {
                iInterfaceLog.setStatus("2");//推送失败
                iInterfaceLogService.update(iInterfaceLog);
                continue;
            }
            //如果返回的json不包含0000，并且请求的内容json不为空
            if ((!iInterfaceLog.getResponseText().contains("0000")) && (iInterfaceLog.getRequestText() != "" && iInterfaceLog.getRequestText() != null)) {
                if (iInterfaceLog.getAppToken() != "" && iInterfaceLog.getAppToken() != null) {

                    String ret = httpSendPostUtil.sendPost(iInterfaceLog.getRequestUrl(), net.sf.json.JSONObject.fromObject(iInterfaceLog.getRequestText()), iInterfaceLog.getAppToken(),"20180117","支付系统推送");
                    iInterfaceLog.setRecallDate(String.valueOf(System.currentTimeMillis()));
                    int num = Integer.valueOf(iInterfaceLog.getRecallNumber() == null ? "0" : iInterfaceLog.getRecallNumber()) + 1;
                    iInterfaceLog.setRecallNumber(String.valueOf(num));
                    iInterfaceLog.setResponseText(ret);
                    iInterfaceLogService.update(iInterfaceLog);
                    System.out.println(ret);
                } else {
                    String ret = httpSendPostUtil.sendPost(iInterfaceLog.getRequestUrl(), net.sf.json.JSONObject.fromObject(iInterfaceLog.getRequestText()),"20180117","支付系统推送");
                    iInterfaceLog.setRecallDate(String.valueOf(System.currentTimeMillis()));
                    int num = Integer.valueOf(iInterfaceLog.getRecallNumber() == null ? "0" : iInterfaceLog.getRecallNumber()) + 1;
                    iInterfaceLog.setRecallNumber(String.valueOf(num));
                    iInterfaceLog.setResponseText(ret);
                    iInterfaceLogService.update(iInterfaceLog);
                    System.out.println(ret);
                }
            }
        }
    }
    @RequestMapping(value = "/again",method = RequestMethod.POST)
    public void executeRealm(@RequestParam(value = "guid") String logGuid){
        //根据guid获取当前的日志
        IInterfaceLog iInterfaceLog = iInterfaceLogService.getByKey(logGuid);
        if( null == iInterfaceLog){
            return;
        }
        //如果返回的json不包含0000，并且请求的内容json不为空
        if ((!iInterfaceLog.getResponseText().contains("0000")) && (iInterfaceLog.getRequestText() != "" && iInterfaceLog.getRequestText() != null)) {
            if (iInterfaceLog.getAppToken() != "" && iInterfaceLog.getAppToken() != null) {

                String ret = httpSendPostUtil.sendPost(iInterfaceLog.getRequestUrl(), net.sf.json.JSONObject.fromObject(iInterfaceLog.getRequestText()), iInterfaceLog.getAppToken(),"20180117","支付系统推送");
                iInterfaceLog.setRecallDate(String.valueOf(System.currentTimeMillis()));
                int num = Integer.valueOf(iInterfaceLog.getRecallNumber() == null ? "0" : iInterfaceLog.getRecallNumber()) + 1;
                iInterfaceLog.setRecallNumber(String.valueOf(num));
                iInterfaceLog.setResponseText(ret);
                iInterfaceLogService.update(iInterfaceLog);
                System.out.println(ret);
            } else {
                String ret = httpSendPostUtil.sendPost(iInterfaceLog.getRequestUrl(), net.sf.json.JSONObject.fromObject(iInterfaceLog.getRequestText()),"20180117","支付系统推送");
                iInterfaceLog.setRecallDate(String.valueOf(System.currentTimeMillis()));
                int num = Integer.valueOf(iInterfaceLog.getRecallNumber() == null ? "0" : iInterfaceLog.getRecallNumber()) + 1;
                iInterfaceLog.setRecallNumber(String.valueOf(num));
                iInterfaceLog.setResponseText(ret);
                iInterfaceLogService.update(iInterfaceLog);
                System.out.println(ret);
            }
        }
    }
}
